/**
 * Search Law APIs
 */
import localforage from "localforage";
import apiClient from "./apiClient";

/*--------------------------------------------------------
 *                   lawData save
 *--------------------------------------------------------*/
//存储数据
async function saveAdvancedSearchData(lawData) {
  await localforage.setItem("searchAdvancedLawData", lawData);
}
//获取数据
export async function getAdvancedSearchData() {
  const lawData = await localforage.getItem("searchAdvancedLawData");
  return lawData;
}

/**
 * 标题搜索 API
 * @param {string} keywords 检索关键词
 * @returns
 */
export async function searchLawTitle(keywords) {
  try {
    const response = await apiClient.post("/search/search-title", { keywords });
    return response.data;
  } catch (err) {
    console.error(err);
    return [];
  }
}

/**
 * 全文搜索 API
 * @param {object} keywords 检索条件
 * @returns
 */
export async function searchFullText(keywords) {
  try {
    const response = await apiClient.post("/search/search-fulltext", {
      keywords,
    });
    const { searchedItem, searchedRoots } = response.data;
    console.log({ searchedItem, searchedRoots });
    return { searchedItem: searchedItem, searchedRoots: searchedRoots };
  } catch (err) {
    console.error(err);
    return [];
  }
}

/**
 * 高级搜索 API
 * @param {*} form 高级搜索的表单项
 * @returns
 */
export async function searchAdvancedFullText(form) {
  try {
    console.log(form);
    const response = await apiClient.post("/search/search-advanced-fulltext", {
      form,
    });
    console.log(response.data);
    const advancedSearchResult = response.data;
    //前端持久化存储
    await saveAdvancedSearchData(advancedSearchResult);
    return "";
  } catch (err) {
    console.error(err);
    return [];
  }
}
